package wsz.edu.ad.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wsz.edu.ad.entity.PromotionAd;
import wsz.edu.ad.entity.PromotionSpace;
import wsz.edu.ad.service.IPromotionAdService;
import wsz.edu.ad.service.IPromotionSpaceService;
import wsz.edu.dto.PromotionAdDTO;
import wsz.edu.dto.PromotionSpaceDTO;
import wsz.edu.remote.AdRemoteService;
import wsz.edu.response.ResponseDTO;
import wsz.edu.util.ConvertUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 远程调用
 * @author wsz
 * @date 2022/4/25 13:53
 **/
@RequestMapping("/ad")
@RestController
public class AdRemoteServiceImpl implements AdRemoteService {

    @Autowired
    private IPromotionSpaceService spaceService;
    @Autowired
    private IPromotionAdService adService;

    @Override
    @GetMapping("/space/getAllSpaces")
    public List<PromotionSpaceDTO> getAllSpaces() {
        List<PromotionSpace> list = spaceService.list();
        return ConvertUtil.convertList(list, PromotionSpaceDTO.class);
    }

    @Override
    @GetMapping("/space/getAdBySpaceKey")
    public List<PromotionSpaceDTO> getAdBySpaceKey(String[] spaceKey) {
        List<PromotionSpaceDTO> dtoList = new ArrayList<>();
        Date now = new Date(System.currentTimeMillis());
        for (String key : spaceKey) {
            // 获取广告位
            QueryWrapper<PromotionSpace> spaceQueryWrapper = new QueryWrapper();
            spaceQueryWrapper.eq("spaceKey", key);
            PromotionSpace promotionSpace = spaceService.getOne(spaceQueryWrapper);

            // 获取广告位绑定的广告
            QueryWrapper<PromotionAd> adQueryWrapper = new QueryWrapper<>();
            adQueryWrapper.eq("spaceId", promotionSpace.getId());
            adQueryWrapper.eq("status", "1");
            adQueryWrapper.lt("startTime", now);
            adQueryWrapper.gt("endTime", now);
            List<PromotionAd> promotionAdList = adService.list(adQueryWrapper);

            // 数据封装
            PromotionSpaceDTO spaceDTO = ConvertUtil.convert(promotionSpace, PromotionSpaceDTO.class);
            spaceDTO.setAdDTOList(ConvertUtil.convertList(promotionAdList, PromotionAdDTO.class));
            dtoList.add(spaceDTO);
        }
        return dtoList;
    }

    @Override
    @PostMapping("/space/saveOrUpdateSpace")
    public ResponseDTO saveOrUpdateSpace(PromotionSpaceDTO spaceDTO) {
        PromotionSpace space = ConvertUtil.convert(spaceDTO, PromotionSpace.class);
        Date now = new Date();
        if (space.getId() == null) {
            space.setCreateTime(now);
            space.setUpdateTime(now);
            space.setIsDel(0);
        } else {
            space.setUpdateTime(now);
        }
        try {
            boolean saveOrUpdate = spaceService.saveOrUpdate(space);
            return ResponseDTO.success(saveOrUpdate);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseDTO.ofError(ex.getMessage());
        }
    }

    @Override
    @GetMapping("/getSpaceById")
    public ResponseDTO getSpaceById(Integer id) {
        PromotionSpace space = spaceService.getById(id);
        return ResponseDTO.success(ConvertUtil.convert(space, PromotionSpaceDTO.class));
    }

    @Override
    @GetMapping("/getAllAds")
    public ResponseDTO getAllAds() {
        List<PromotionAd> list = adService.list();
        return ResponseDTO.success(ConvertUtil.convertList(list, PromotionAdDTO.class));
    }

    @Override
    @PostMapping("/saveOrUpdateAd")
    public ResponseDTO saveOrUpdateAd(PromotionAdDTO adDTO) {
        PromotionAd ad = ConvertUtil.convert(adDTO, PromotionAd.class);

        Date now = new Date();
        if (ad.getId() == null) {
            ad.setCreateTime(now);
            ad.setUpdateTime(now);
            ad.setStatus(1);
        } else {
            ad.setUpdateTime(now);
        }
        try {
            boolean saveOrUpdate = adService.saveOrUpdate(ad);
            return ResponseDTO.success(saveOrUpdate);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseDTO.ofError(ex.getMessage());
        }
    }

    @Override
    public ResponseDTO getAdById(Integer id) {
        PromotionAd ad = adService.getById(id);
        return ResponseDTO.success(ConvertUtil.convert(ad, PromotionAdDTO.class));
    }
}
